package 代码记录.指定公司代码练习记录.西安.分值100分;

import java.util.*;

/**
 * @author zx
 * @create 2023-06-27 16:35
 * 改的人家的思路
 */
public class 阿里巴巴找黄金宝箱4 {//copy(系列最难的，自己肯定做不成)
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        List<Integer> list = new ArrayList<>();
        String[] arr = str.split(",");
        for (int i = 0; i < arr.length; i++) {
            list.add(Integer.parseInt(arr[i]));
        }
        int[] temp = helper(list);
        String res = "";

        for (int i = 0;i < temp.length;i++) {
            res += temp[i] + ",";
        }

        System.out.println(res.substring(0, res.length() - 1));
    }
    public static int[] helper(List<Integer> list) {
        Deque<Integer> stack = new ArrayDeque<>();
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            while (!stack.isEmpty() && list.get(stack.peekFirst()) < list.get(i)) {
                int index = stack.pop();
                res[index] = list.get(i);
            }
            stack.push(i);
        }
        while (!stack.isEmpty()) {
            boolean flag = false;
            int index = stack.pop();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) > list.get(index)) {
                    flag = true;
                    res[index] = list.get(i);
                    break;
                }
            }
            if (!flag) {
                res[index] = -1;
            }
        }
        return res;
    }
}

